﻿/**
* @description  Factory class used to instantiate Slide instances for a slide show.
*/
package com.foed.applications.slideshow.slides {

	import flash.utils.getDefinitionByName;

	import com.foed.applications.slideshow.SlideShow;
	import com.foed.applications.slideshow.slides.BlankSlide;
	import com.foed.applications.slideshow.slides.Slide;

	public class SlideFactory {
		
		static private const PACKAGE_PATH_ATTRIBUTE:String = "packagePath";
		static private const CLASS_NAME_ATTRIBUTE:String = "className";
	
		/**
		* @description  Returns a Slide instance based on the XML passed.
		*
		* @param  pXML  The XML defining a slide that is used to determine the proper Slide child class to instantiate.
		* @param  pSlideShow  The main slide show application.
		*
		* @returns  A Slide instance as determined by the XML passed in.
		*/
		static public function createSlide(pXML:XML, pSlideShow:SlideShow):Slide {
			var pNodeName:String = pXML.name().toString();
			var pClassMappings:XML = pSlideShow.classMappings;
			var pPackagePath:String = pClassMappings.@[PACKAGE_PATH_ATTRIBUTE];
			var pClassName:String = pClassMappings.child(pNodeName)[0].@[CLASS_NAME_ATTRIBUTE];
			if (pPackagePath.length > 0) pClassName = pPackagePath + "." + pClassName;
			var pClass:Class;
			try {
				pClass = getDefinitionByName(pClassName) as Class;
			} catch (e:Error) {
				trace(e.getStackTrace());
			} finally {
				if (pClass == null) pClass = BlankSlide;
				return new pClass(pXML, pSlideShow) as Slide;
			}
		}
	
	}

}